<?php
//消费券及领取操作

namespace App\Repositories;

use App\Models\AdminModel;

class AdminRepository extends BaseRepository
{
    /**
     * @param $params
     * @return array[]
     */
    public function getFilter ($params) {
        $where = [];
        if ($params['status']) {
            $where[] = [
                'status', $params['status']
            ];
        }
        if ($params['name']) {
            $where[] = [
                'name', 'like', '%' . $params['name'] . '%'
            ];
        }
        if ($params['mobile']) {
            $where[] = [
                'mobile', 'like', '%' . $params['mobile'] . '%'
            ];
        }
        if ($params['email']) {
            $where[] = [
                'email', 'like', '%' . $params['email'] . '%'
            ];
        }
        return [
            'where' => $where,
        ];
    }

    /**
     * @param $params
     * @return array
     */
    public function getList ($params)
    {
        $filter = $this->getFilter($params);
        $list = $this->getListItems(AdminModel::query(), $filter, $params['page_size'] ?? 20, ['id', 'name', 'email', 'mobile', 'status']);
        return $list;
    }

    /**
     * @param $params
     */
    public function addOrUpdate ($params)
    {
        if (!empty($params['id'])) {
            $rst = $this->_updateAdmin($params);
        } else {
            $rst = $this->_addAdmin($params);
        }
        return $rst;
    }

    /**
     * @param $params
     */
    private function _updateAdmin ($params)
    {
        return AdminModel::where('id', $params['id'])
            ->update([
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'email' => $params['email'],
                'status' => $params['status'],
            ]);
    }

    /**
     * @param $params
     * @return mixed
     */
    private function _addAdmin ($params)
    {
        $admin = new AdminModel();
        $admin->name = $params['name'];
        $admin->mobile = $params['mobile'];
        $admin->status = $params['status'];
        $admin->email = $params['email'];
        return $admin->save();
    }

    public function incrLoginErrNum ($id, $reset = false)
    {
        $admin = AdminModel::find($id);
        if (!$admin) {
            return false;
        }
        if ($reset) {
            $admin->error_login_number = 0;
            $res = $admin->save();
        } else {
            $res = $admin->increment('error_login_number', 1);
        }
        if (!$res) {
            return false;
        }
    }

    /**
     * @param $allParams
     * @return mixed
     */
    public function savePassword ($allParams)
    {
        $adminId = $allParams['id'];
        $admin = AdminModel::find($adminId);
        if (empty($admin['salt'])) {
            $admin->salt = getRandomString();
        }
        $admin->password = getEncryptPassword($allParams['password'], $admin->salt);
        $admin->updated_at = date('Y-m-d H:i:s');
        return $admin->save();

    }
}
